﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Text;
using System.Windows.Forms;
using LayerEntity.Almacen;
using LayerEntity.Producto;

namespace System2012.Listados
{
    /// <summary>
    /// 
    /// </summary>
    public partial class frmBusquedaProductos : DevExpress.XtraEditors.XtraForm
    {
        /// <summary>
        /// 
        /// </summary>
        DataTable dtbProducto;
        /// <summary>
        /// 
        /// </summary>
        DataTable dtbPrecios;
        /// <summary>
        /// 
        /// </summary>
        int indexProd;
        /// <summary>
        /// 
        /// </summary>
        int indexPre;
        /// <summary>
        /// Almacenaos todos los datos del producto de la lista seleccionada
        /// </summary>
        public ArrayList data;// = new ArrayList();
         
        #region
        public int Opcion { get; set; }
        #endregion
        public frmBusquedaProductos()
        {
            this.InitializeComponent();
        }

        private void Seleccionar()
        {
            this.data = new ArrayList();
            if (this.Opcion == 1)
            {
                if (this.indexProd >= 0)
                {
                    this.data.Add(this.dtbProducto.Rows[this.indexProd]["id_producto"].ToString());
                    this.Close();
                }
            }
            else
            {
                if (this.indexProd >= 0)
                {
                    this.data.Add(this.dtbProducto.Rows[this.indexProd]["id_producto"].ToString());//0
                    this.data.Add(this.dtbProducto.Rows[this.indexProd]["producto"].ToString());//1
                    this.data.Add(this.dtbProducto.Rows[this.indexProd]["cod_bar"].ToString());//2
                    this.data.Add(this.dtbProducto.Rows[this.indexProd]["inc_serie"].ToString());//3
                    this.data.Add(this.dtbProducto.Rows[this.indexProd]["prod_serv"].ToString());//4
                    this.data.Add(this.dtbProducto.Rows[this.indexProd]["impuesto"].ToString());//5
                    this.data.Add(this.dtbProducto.Rows[this.indexProd]["controla_stock"].ToString());//6
                    this.data.Add(this.dtbProducto.Rows[this.indexProd]["stock"].ToString());//7
                    if (this.dtbPrecios.Rows.Count == 1)
                    {
                        this.data.Add(this.dtbPrecios.Rows[0]["id_unidad"].ToString());//8
                        this.data.Add(this.dtbPrecios.Rows[0]["id_lista"].ToString());//9
                        this.data.Add(this.dtbPrecios.Rows[0]["id_precios"].ToString());//10
                        this.data.Add(this.dtbPrecios.Rows[0]["abreviatura"].ToString());//unidad//11
                        this.data.Add(this.dtbPrecios.Rows[0]["precio1"].ToString());//12
                        this.data.Add(this.dtbPrecios.Rows[0]["precio2"].ToString());//13
                        this.data.Add(this.dtbPrecios.Rows[0]["precio3"].ToString());//14
                        this.data.Add(this.dtbPrecios.Rows[0]["precio4"].ToString());//15
                        this.data.Add(this.dtbPrecios.Rows[0]["precio5"].ToString());//16
                        this.data.Add(this.dtbPrecios.Rows[0]["val_unidad"].ToString());//17
                        this.data.Add(this.dtbPrecios.Rows[0]["val_minimo"].ToString());//18
                        this.Close();
                    }                   
                }
            }
        }

        private void frmBusquedaProductos_Load(object sender, EventArgs e)
        {
            if (!this.BGWProductos.IsBusy)
            {
                this.BGWProductos.RunWorkerAsync();
            }
        }

        private void BGWProductos_DoWork(object sender, DoWorkEventArgs e)
        {
            this.BGWProductos.ReportProgress(30, "Cargando Familias");
            EProducto obj = new EProducto();
            obj.descripcion = string.Format("where (id_producto || familia || producto) ilike '%{0}%'", this.txtFiltro.Text);
            e.Result = SystemUtils.RecodManajerproducto.GetListaProductos(obj);
            this.BGWProductos.ReportProgress(100, "Completado");
        }

        private void BGWProductos_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            this.dtbProducto = (DataTable)e.Result;
            this.tblGridProducto.DataSource = this.dtbProducto;
            //if (!BGWListaPrecios.IsBusy) BGWListaPrecios.RunWorkerAsync();
        }

        private void txtFiltro_TextChanged(object sender, EventArgs e)
        {
            if (!this.BGWProductos.IsBusy)
            {
                this.BGWProductos.RunWorkerAsync();
            }
        }

        private void BGWListaPrecios_DoWork(object sender, DoWorkEventArgs e)
        {
            if (this.indexProd >= 0)
            {
                this.BGWListaPrecios.ReportProgress(30, "Cargando Precios");
                EPrecios obj = new EPrecios();
                string idprod = this.dtbProducto.Rows[this.indexProd]["id_producto"].ToString();
                obj.Condicion = string.Format("where id_producto='{0}'", idprod);
                e.Result = SystemUtils.RecodManajerproducto.GetListaPrecios(obj);
                this.BGWListaPrecios.ReportProgress(100, "Completado");
            }
        }

        private void BGWListaPrecios_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            this.dtbPrecios = (DataTable)e.Result;
            this.gridControl3.DataSource = e.Result;
            if (!this.BGWListStocks.IsBusy)
            {
                this.BGWListStocks.RunWorkerAsync();
            }
        }

        private void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            this.indexProd = e.FocusedRowHandle;
            if (!this.BGWProductos.IsBusy)
            {
                if (!this.BGWListaPrecios.IsBusy)
                {
                    this.BGWListaPrecios.RunWorkerAsync();
                }
            }
        }

        private void txtFiltro_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                if (this.gridView1.RowCount > 0)
                {
                    if (this.dtbProducto.Rows.Count > 0)
                    {
                        this.tblGridProducto.Focus();
                    }
                }
            }
        }

        private void tblGridProducto_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                this.Seleccionar();
            }
        }

        private void BGWProductos_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            // barEditItem1.EditValue = e.ProgressPercentage;
        }

        private void tblGridProducto_DoubleClick(object sender, EventArgs e)
        {
            this.Seleccionar();
        }

        private void txtFiltro_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Down)
            {
                if (this.gridView1.RowCount > 0)
                {
                    if (this.dtbProducto.Rows.Count > 0)
                    {
                        this.tblGridProducto.Focus();
                    }
                }
            }
        }

        private void BGWListStocks_DoWork(object sender, DoWorkEventArgs e)
        {
            if (this.indexProd >= 0)
            {
                string idprod = this.dtbProducto.Rows[this.indexProd]["id_producto"].ToString();
                EAlmacen objAlm = new EAlmacen();
                StringBuilder cadena = new StringBuilder();
                cadena.Append("where id_producto='");
                cadena.AppendFormat("{0}'", idprod);
                Console.Write(string.Format("{0}\n", cadena));
                objAlm.Condicion = cadena.ToString();
                e.Result = System2012.SystemUtils.RecordmanagerAlmacen.GetListaStockAlamcen(objAlm);
            }
        }

        private void BGWListStocks_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            this.gridControl2.DataSource = e.Result;
        }
    }
}